package com.yxm.useryxm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxm.useryxm.model.domain.User;
import com.yxm.useryxm.service.UserService;
import com.yxm.useryxm.mapper.UserMapper;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.yxm.useryxm.contance.UserContance.USER_LOGIN_STATUS;

/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-11-24 16:19:41
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //@Resource
//private UserMapper userMapper;
//    盐值
    public static final String SALT = "yxm";



    @Override
    public long userRegister(String userAccount, String userPassWord, String checkPassword) {
        if (StringUtils.isAnyBlank(userAccount, userPassWord, checkPassword)) {
            return -1;
        }
        if (userAccount.length() < 4) {
            return -1;
        }
        if (userPassWord.length() < 7 || checkPassword.length() < 7) {
            return -1;
        }

        //不能包含特殊字符
        String validPattern = "[^\\w\\s\\u4e00-\\u9fa5]";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
        if (matcher.find()) {
            return -1;
        }
        if (!userPassWord.equals(checkPassword)) {
            return -1;
        }

        //用户名不能重复
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_Account", userAccount);
        long count = this.count(queryWrapper);
        if (count > 0) {
            return -1;
        }
        //加密密码

        String encryptPassword = DigestUtils.md5DigestAsHex((userPassWord + SALT).getBytes());

        //插入用户
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPassword(encryptPassword);
        boolean result = this.save(user);
        if (!result) {
            return -1;
        }
        return user.getId();
    }

    @Override
    public User userLogin(String userAccount, String userPassWord, HttpServletRequest request) {
        if (StringUtils.isAnyBlank(userAccount, userPassWord)) {
            return null;
        }
        if (userAccount.length() < 4) {
            //todo修改为自定义异常
            return null;
        }
        if (userPassWord.length() < 7) {
            return null;
        }
        //不能包含特殊字符
        String validPattern = "[^\\w\\s\\u4e00-\\u9fa5]";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
        if (matcher.find()) {
            return null;
        }

        //2.加密密码
        String encryptPassword = DigestUtils.md5DigestAsHex((userPassWord + SALT).getBytes());
        //用户名不能重复
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", userAccount);
        queryWrapper.eq("user_password", encryptPassword);
        User user = this.getOne(queryWrapper);
        if (user == null) {
            log.info("用户登录失败，未找到用户信息");
            return null;
        }
        //脱敏
        User safetytUser = getSafetytUser(user);
        //3.记录用户的登录态
        request.getSession().setAttribute(USER_LOGIN_STATUS, safetytUser);
        return safetytUser;
    }

    /**
     * 用户脱敏
     * @param user
     * @return
     */
    @Override
    public  User getSafetytUser(User user) {
        User safetytUser = new User();
        safetytUser.setId(user.getId());
        safetytUser.setUserName(user.getUserName());
        safetytUser.setUserAccount(user.getUserAccount());
        safetytUser.setAvatarUrl(user.getAvatarUrl());
        safetytUser.setGender(user.getGender());
        safetytUser.setPhone(user.getPhone());
        safetytUser.setEmail(user.getEmail());
        safetytUser.setUserRole(user.getUserRole());
        safetytUser.setUserStatus(user.getUserStatus());
        safetytUser.setCreateTime(user.getCreateTime());
        return safetytUser;
    }
}




